<template>
  <div class="search-phones">
    <!-- 搜索|新增 -->
    <div class="set-parameters">
      <el-input
        v-model="wx_id"
        size="small"
        placeholder="请输入wxid"
        clearable
        maxlength="30"
        show-word-limit
        style="width: 220px"
      ></el-input>
      <el-input
        v-model="wx_name"
        size="small"
        placeholder="请输入微信名称"
        clearable
        maxlength="30"
        show-word-limit
        style="width: 220px"
      ></el-input>
      <el-button type="primary" size="small" @click="queryData">查询</el-button>
    </div>

    <!-- 信息展示 -->
    <el-table :data="tableData" stripe style="width: 100%" height="60px">
      <el-table-column prop="wx_id" label="wxid" header-align="center" align="center"></el-table-column>
      <el-table-column prop="wx_name" label="微信昵称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="create_time" label="拉黑时间" header-align="center" align="center">
        <template slot-scope="scope">
          {{getTime(scope.row.create_time)}}
        </template>
      </el-table-column>
      <el-table-column prop="create_account" label="操作账号" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" header-align="center" align="center" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            style="background-color: #ff9f00; color: #ffffff"
            type="warning"
            @click="handleDelete(scope.$index, scope.row)"
          >
            取消拉黑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="pageNo"
      :page-sizes="[20, 40, 60, 80]"
      :page-size="pageSize"
      :pager-count="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>

  </div>
</template>

<script>
import { mapState } from 'vuex'
import { blackPageList,delBlack } from '@/api/admin/custom'
import dayjs from 'dayjs'

export default {
  name: 'AccountManagement',
  data() {
    return {
      tableData: [], // 表格中的数据
      accountName: '', // 账号名称
      pageNo: 1, // 第几页
      pageSize: 20, // 每页几个数据
      total: 0, // 总
      wx_id:'',
      wx_name:''
    }
  },
  computed: {
    ...mapState({
      adminInfo: 'adminInfo'
    })
  },
  watch: {
    accountName(val) {
      if (val.trim() === '') {
        this.queryData()
      }
    }
  },
  mounted() {
    this.queryDataSilence()
  },
  methods: {
    getTime(long){
      return dayjs(long).format('YYYY-MM-DD HH:mm')
    },
    // 分页查询数据
    queryData() {
      let data={ wx_id: this.wx_id, wx_name: this.wx_name, page: this.pageNo, rows: this.pageSize }
      blackPageList(data).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data.rows || []
          this.total = res.data.records || 0
          this.$message({
            type: 'success',
            message: '查询成功！',
            duration: 1000
          })
        }
      })
    },
    queryDataSilence() {
      let data={ wx_id: this.wx_id, wx_name: this.wx_name, page: this.pageNo, rows: this.pageSize }
      blackPageList(data).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data.rows || []
          this.total = res.data.records || 0
        }
      })
    },
    // 修改pageSize
    handleSizeChange(val) {
      if (this.pageSize === val) return
      this.pageSize = val
      this.queryData()
    },
    // 修改当前页
    handleCurrentChange(val) {
      this.pageNo = val
      this.queryData()
    },
    // 删除
    handleDelete(index, row) {
      this.$confirm('是否删除客服账号！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delBlack(row.wx_id).then((res) => {
            if (res.code === 0) {
              const pn = Math.ceil((this.total - 1) / this.pageSize)
              if (this.pageNo > pn) {
                this.pageNo = pn
              }
              this.queryDataSilence()
              this.$message({
                type: 'success',
                message: '删除成功!',
                duration: 1000
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消',
            duration: 1000
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-phones {
  height: 100%;
  display: flex;
  flex-direction: column;

  .set-parameters {
    padding: 10px 5px;
    display: flex;
    align-items: center;
    border-bottom: solid $border-color-1 2px;
    .el-button {
      margin-left: 20px;
    }
  }

  .el-pagination {
    padding: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: solid 1px $border-color-1;
    background-color: white;
  }

  ::v-deep .el-dialog {
    width: 500px;
    .el-form {
      width: 400px;
    }
  }
}
</style>
